<?php
include_once("sessionmanager.php");
initSession();

if (!isset($_SESSION['s_loggedin']) || ($_SESSION['s_loggedin'] == false) || !isset($_SESSION['s_serverurl']) || (strpos($_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'],$_SESSION['s_serverurl']) === false)) {
	$error_page_code = "timeout";
	include "errorpage.php";
	exit();
}
if (!$_SESSION['s_cansearch']) {
	$error_page_code = "permission";
	include "errorpage.php";
	exit();
}
include "languages.php";
include_once "functions.inc.php";
include "attributes.php";
$userid = $_SESSION['s_userid'];

makeheader(PPTRENDSVIEW_TITLE);
require "config.php";

if (isset($_GET['unknown'])) {
	
	mysql_connect($dbhost,$dbusername,$dbpassword) or die(PARSER_ERROR3);
	mysql_select_db($dbname) or die(PARSER_ERROR2);

} else if (!isset($_GET['unknown'])) {
	
	mysql_connect($dbhost,$dbusername,$dbpassword) or die(PARSER_ERROR3);
	mysql_select_db($dbname) or die(PARSER_ERROR2);
	$query_sel = "SELECT c.*, u.status, u.frank, u.fpoints, u.rrank, u.rpoints, u.last_update as entrylast_update FROM $pptrends c left join $pstablename u on (c.userid=u.userid and c.playername=u.playername) ";
	$query = "";
	$query .= " WHERE c.userid=$userid ";
	if(isset($_GET['ally']) && !empty($_GET['ally'])) $query .= " AND allyname='" . $_GET['ally'] ."'";
	if(isset($_GET['player']) && !empty($_GET['player'])) $query .= " AND c.playername like '" . $_GET['player'] ."%'";
	else $query .=" AND cast(c.last_update AS DATETIME)>DATE_SUB(now(), INTERVAL 3 DAY) ";
	if(isset($_GET['gepoints']) && !empty($_GET['gepoints']) && is_numeric($_GET['gepoints'])) $query .= " AND c.points>=" . $_GET['gepoints'] ."";
	if(isset($_GET['lepoints']) && !empty($_GET['lepoints']) && is_numeric($_GET['lepoints'])) $query .= " AND c.points<=" . $_GET['lepoints'] ."";
	if(isset($_GET['lcgalaxy']) && !empty($_GET['lcgalaxy']) && is_numeric($_GET['lcgalaxy']) &&
	   isset($_GET['lcsolar']) && !empty($_GET['lcsolar']) && is_numeric($_GET['lcsolar']) &&
	   isset($_GET['lcround']) && is_numeric($_GET['lcround'])) {
	   	$lcgalaxy = (int)$_GET['lcgalaxy'];
	   	$lcsolar = (int)$_GET['lcsolar'];
	   	$lcround = (int)$_GET['lcround'];
	   	if($lcgalaxy >=1 && $lcgalaxy <=9 && $lcsolar >=1 && $lcsolar <=499 && $lcround>=0 && $lcround<=500) {
	   		$query_sel .= " left join $dbtablename b2 on (b2.spielername=c.playername and b2.userid=c.userid) ";
	   		//mysql does not support subquery such as "exists" before version 4.1.0
	   		//$query .= " AND EXISTS(select coordinates from $dbtablename b2 where b2.spielername=c.playername and b2.userid=c.userid and b2.galaxie=$lcgalaxy and b2.system between " . (string)($lcsolar-$lcround) . " and " . (string)($lcsolar+$lcround) . ") ";
	   		$query .= " AND b2.galaxie=$lcgalaxy and b2.system between " . (string)($lcsolar-$lcround) . " and " . (string)($lcsolar+$lcround) . " ";
	   	}
	}
	$query = $query_sel . $query;
	$query .= " ORDER BY c.rank ASC";
	if(isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page']>0) $page = $_GET['page'];
	else $page=1;
	$pagecount = 50;
	$query .= " LIMIT ". (($page-1)*$pagecount) . ", " . ($pagecount+1);
	$res = mysql_query($query);
	if(!$res) {
		echo mysql_error();
		return;
	}
	echo '<table cellpadding="0" cellspacing="2" border="0" align="center" width="95%">';
	$rows = mysql_num_rows($res);

	if ( $rows == 0) {
		echo "<tr class=\"tblhead\"><td colspan=\"15\">"."Player rank view"."</td></tr>\n";
		echo "<tr class=\"firstcolor\"><td colspan=\"15\">"."No data"."</td></tr>\n";
	} else {
		$i = 0;
		while (($line = mysql_fetch_array($res)) && ($i<$pagecount)) {
			if($i == 0) {
				echo "<tr class=\"tblhead\"><td colspan=\"15\">"."Player rank view (List All)"."</td></tr>\n";
				echo "<tr class=\"tblhead\"><td>Player</td><td>".">"."</td><td>".">"."</td><td>".">"."</td><td>".">"."</td><td>".">"."</td><td>".">"."</td><td>".">"."</td><td>".">"."</td><td>".">"."</td><td>".">"."</td><td>".">"."</td><td>".">"."</td><td>".">"."</td><td>".">"."</td></tr>";
			}
			echo "<tr class=\"firstcolor\" align=\"center\">\n";
			$ind = $line["ind"];
			//$phint = "'this.T_WIDTH=250;return escape(\"".htmlentities("Rank: ". $line["rank_$ind"] . "<br/>ally: " . trim($line['allyname']) . "<br/>Updated: " . $line["last_update"]) . "\")'";
			$phint = "'this.T_WIDTH=250;return escape(\"".htmlentities("<div style=font-size:10pt;>" . $line['playername'] . ":</div><br/>ally: " . trim($line['allyname']) . "<br/>" . SHOW_RANK . ": #" . $line["rank_$ind"] . " - " . STATS_SCORE . ": " . number_format($line["points_$ind"],0,",",".") . "<br>" . STATS_FLEET . ": #" . $line['frank'] . " - " . STATS_SCORE . ": " . number_format($line['fpoints'],0,",","."). "<br>" . STATS_RESEARCH . ": #" . $line['rrank'] . " - " . STATS_SCORE . ": " . number_format($line['rpoints'],0,",",".")) . "<br><br>" . STATS_UPDATE . ": " . $line['last_update'] . "\")'";
			echo "<td style=\"width:120;\" align=\"left\">"."<a class=\"link\" href=\"show.php?spieler=". $line['playername'] . "&new=true\" onmouseover=$phint >" .$line['playername'] ."</a>". (empty($line['status']) ? "" : " (<span style=\"color:#a0ffa0;\">".$line['status']."</span>)") . "</td>\n";
			$nind = ($ind+1==15 ? 1 : $ind+1);
			for($j=0; $j<14; $j++) {
				$oind = $nind;
				$nind = ($ind+1+$j >= 15 ? $ind+1+$j-15+1 : $ind+1+$j);
				$np = $line["points_$nind"];
				$op = $line["points_$oind"];
				if($np == 0) $points = '&nbsp;';
				else if($nind != $oind)
					$points = ($np == $op ? "<span style=\"color:#a0ffa0;font-weight:bold;\">*</span>" : ($op>0 && $np<$op ? "<span style=\"color:#a0ffa0;\">$np</span>" : $np));
				else $points = $np;
				echo "<td align=\"center\">"."<acronym style=\"border-width:0;\" title=\"" . $line["update_$nind"] . "\">" .$points. "</acronym></td>\n";
			}
			echo "</tr>";
			
			$i++;
		}
		$qs = preg_replace('/&?page=[^%&\s]*/', '',$_SERVER['QUERY_STRING']);
		if(!empty($qs)) $qs = $qs . "&";
		echo "<tr class=\"tblhead\"><td colspan=\"15\">" . 
		($page>1 ? "<a class=\"link\" href=\"pptrendsview.php?$qs" . "page=". ($page-1) ."\">prev&lt;</a>" : "") . 
		"&nbsp;$page&nbsp;" .
		($rows>$pagecount ? "<a class=\"link\" href=\"pptrendsview.php?$qs". "page=". ($page+1) ."\">&gt;next</a>" : "") .
		"</td></tr>\n";
		echo "<tr class=\"tblhead\"><td colspan=\"15\">("."END".")</td></tr>\n";
   	
	}
	echo "<tr class=\"tblhead\">" . "<form method=\"GET\" action=\"pptrendsview.php\">". 
	"<td colspan=\"15\" align=\"left\">". 
	"search by points: between" . 
	"<input class=\"textfield\" name=\"gepoints\" value=\"". $_GET['gepoints'] ."\" style=\"width:60px;height:15px;\" maxlength=\"15\">". 
	" and ".
	"<input class=\"textfield\" name=\"lepoints\" value=\"". $_GET['lepoints'] ."\" style=\"width:60px;height:15px;\" maxlength=\"15\">". 
	"</td>" . "<input type=\"submit\" style=\"display:none;\"/> </form>" . "</tr>\n" .
	"<tr class=\"tblhead\">" . "<form method=\"GET\" action=\"pptrendsview.php\">". "<td colspan=\"15\" align=\"left\">". 
	"search by playername: " . 
	"<input class=\"textfield\" name=\"player\" value=\"". $_GET['player'] ."\" style=\"width:80px;height:15px;\" maxlength=\"30\" >". 
	"</td>" . "</form>" . "</tr>\n" .
	"<tr class=\"tblhead\">" . "<form method=\"GET\" action=\"pptrendsview.php\">". 
	"<td colspan=\"15\" align=\"left\">". 
	"search by location: around galaxy" . 
	"<input class=\"textfield\" name=\"lcgalaxy\" value=\"". $_GET['lcgalaxy'] ."\" style=\"width:15px;height:15px;\" maxlength=\"1\">". 
	"and solar".
	"<input class=\"textfield\" name=\"lcsolar\" value=\"". $_GET['lcsolar'] ."\" style=\"width:30px;height:15px;\" maxlength=\"3\">". 
	" , (+/-) ".
	"<input class=\"textfield\" name=\"lcround\" value=\"". $_GET['lcround'] ."\" style=\"width:30px;height:15px;\" maxlength=\"3\">". 
	"</td>" . "<input type=\"submit\" style=\"display:none;\"/> </form>" . "</tr>\n";
	echo "</table>";
}
?>

<?php
makefooter();
?>
